import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/management_operation_page/change_shifts/change_shifts_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 交接班页面
class ChangeShiftsPage extends StatelessWidget {
  ChangeShiftsPage({super.key});

  final controller = Get.put(ChangeShiftsController());
  /// 作业交接内容
  Widget _getJobHandoverContent() {
    return Container(
      color: Colors.white,
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '作业交接内容',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
              color: const Color(0xff262F38),
            ),
          ),
          AppGaps.vGap16,
          Container(
              height: 135.w,
              decoration: BoxDecoration(
                color: const Color(0xffF7F7FB),
                borderRadius: BorderRadius.all(Radius.circular(4.w)),
                border: Border.all(width: 1.w, color: const Color(0xffEBEDF0)),
              ),
              child: TextField(
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.done,
                  maxLines: null,
                  onChanged: (value) {
                    controller.handoverContent = value;
                  },
                  style:
                      TextStyle(color: const Color(0xFF262F38), fontWeight: FontWeight.w400, fontSize: 14.w),
                  decoration: InputDecoration(
                    counterText: '',
                    //防止适配不居中
                    isCollapsed: true,
                    border: const OutlineInputBorder(borderSide: BorderSide.none),
                    contentPadding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 9.w),
                    hintText: '请输入交接内容...',
                    hintStyle: TextStyle(
                        fontSize: 14.w, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                  ))),
        ],
      ),
    );
  }

  /// 根据数据显示人员选择
  /// [operatorName] 类型名称
  /// [personnelList]数据
  /// [isSuccessor]是否为接班人
  Widget _getPersonnelSelectionView(
      String operatorName, List<ExecuteTicketReceiptListOperatorLists> personnelList,
      {bool isSuccessor = false}) {
    // 监护人单独选择
    if (operatorName == '监护人') {
      return Container();
    }
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              constraints: BoxConstraints(
                minWidth: 90.w,
              ),
              child: Text(
                operatorName,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff262F38),
                ),
              ),
            ),
            Expanded(
              child: Wrap(
                spacing: 10.w,
                runSpacing: 10.w,
                children: personnelList
                    .map((item) => Container(
                          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 3.w),
                          decoration: BoxDecoration(
                            color: AppColors.bg_color_grey,
                            borderRadius: BorderRadius.circular(7.w),
                            border: Border.all(
                              color: AppColors.blue_button,
                              width: 1.w,
                            ),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                item.operatorUserName!,
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  color: AppColors.blue_button,
                                ),
                              ),
                              AppGaps.hGap5,
                              Icon(Icons.close, size: 14.sp, color: AppColors.blue_button),
                            ],
                          ),
                        ).click(() {
                          personnelList.remove(item);
                          controller.update();
                        }))
                    .toList(),
              ),
            ),
            Container(
                width: 40.w,
                height: 20.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff5C6DFF),
                  borderRadius: BorderRadius.circular(7.w),
                ),
                child: Text(
                  '添加',
                  style: TextStyle(
                    fontSize: 10.sp,
                    color: Colors.white,
                  ),
                )).clickOnce(() {
              // 是否为选择接班人
              if (isSuccessor) {
                controller.addSuccessorPersonnel(operatorName);
              } else {
                controller.addHandoverPersonnel(operatorName, personnelList);
              }
            }),
          ],
        ),
        Container(
          margin: EdgeInsets.symmetric(vertical: 20.w),
          height: 1.w,
          color: const Color(0xff262F38).withOpacity(0.1),
        ),
      ],
    );
  }

  /// 监护人选择签字
  /// [isSuccessor]是否为接班人
  Widget _getGuardianView(List<ExecuteTicketReceiptListOperatorLists> guardianList,
      {bool isSuccessor = false}) {
    return Column(
      children: [
        // 标题
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '监护人',
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff262F38),
              ),
            ),
            Container(
                width: 40.w,
                height: 20.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff5C6DFF),
                  borderRadius: BorderRadius.circular(7.w),
                ),
                child: Text(
                  '添加',
                  style: TextStyle(
                    fontSize: 10.sp,
                    color: Colors.white,
                  ),
                )).clickOnce(() {
              // 是否为选择接班人
              if (isSuccessor) {
                controller.addSuccessorPersonnel('监护人');
              } else {
                controller.addHandoverPersonnel('监护人', guardianList);
              }
            })
          ],
        ),
        AppGaps.vGap20,
        // 监护人选择
        ListView.separated(
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemBuilder: (context, index) {
              final ExecuteTicketReceiptListOperatorLists item = guardianList[index];
              return Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        item.operatorUserName!,
                        style: TextStyle(
                          fontSize: 13.sp,
                          color: AppColors.blue_button,
                        ),
                      ),
                      const Icon(
                        Icons.highlight_remove,
                        color: Color(0xFFFD6B6B),
                      ).clickOnce(() {
                        item.operatorUserSign = '';
                        guardianList.remove(item);
                        controller.update();
                      })
                    ],
                  ),
                  AppGaps.vGap10,
                  Container(
                    alignment: Alignment.center,
                    height: 36.w,
                    decoration: BoxDecoration(
                      color: const Color(0xffF7F7FB),
                      borderRadius: BorderRadius.all(Radius.circular(4.w)),
                      border: Border.all(width: 1.w, color: const Color(0xffEBEDF0)),
                    ),
                    child: item.operatorUserSign!.isEmpty
                        ? Text('点击此处签名',
                            style: TextStyle(
                              fontSize: 14.sp,
                              height: 1.2,
                              color: AppColors.font_color_gray,
                            ))
                        : Container(
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(4.w),
                                color: Colors.white,
                                image: DecorationImage(
                                  image: NetworkImage(Interface.fileUrl + item.operatorUserSign!),
                                )),
                          ),
                  ).clickOnce(() {
                    Get.toNamed(AppRoutes.signature)?.then((value) {
                      //如果不为空则设置签名
                      if (value != null) {
                        item.operatorUserSign = value.toString();
                      }
                      controller.update();
                    });
                  })
                ],
              );
            },
            separatorBuilder: (context, index) {
              return Container(
                margin: EdgeInsets.symmetric(vertical: 20.w),
                height: 1.w,
                color: const Color(0xff262F38).withOpacity(0.1),
              );
            },
            itemCount: guardianList.length)
      ],
    );
  }

  /// 获取交班人显示
  Widget _getHandoverView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(
          '交班人',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
            color: const Color(0xff262F38),
          ),
        ),
        AppGaps.vGap16,
        // 人员显示
        ...controller.selectedHandoverPersonMap.entries.map((item) {
          final operatorName = item.key;
          final operatorList = item.value;
          return _getPersonnelSelectionView(operatorName, operatorList);
        }),
        // 监护人
        _getGuardianView(controller.selectedHandoverPersonMap['监护人']!),
      ]),
    );
  }

  /// 获取接班人信息
  Widget _getSuccessorView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(
          '接班人',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
            color: const Color(0xff262F38),
          ),
        ),
        AppGaps.vGap16,
        // 人员显示
        ...controller.selectedSuccessorMap.entries.map((item) {
          final operatorName = item.key;
          final operatorList = item.value;
          return _getPersonnelSelectionView(operatorName, operatorList, isSuccessor: true);
        }),
        // 监护人
        _getGuardianView(controller.selectedSuccessorMap['监护人']!, isSuccessor: true),
      ]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '交接班',
      ),
      body: GetBuilder(
          init: controller,
          builder: (_) {
            if (controller.selectedHandoverPersonMap.isEmpty) {
              return Container();
            }
            return SingleChildScrollView(
              child: Column(
                children: [
                  // 作业交接内容
                  _getJobHandoverContent(),
                  AppGaps.vGap10,
                  // 交班人信息
                  _getHandoverView(),
                  // 接班人信息
                  _getSuccessorView(),
                  AppGaps.vGap60,
                ],
              ),
            );
          }),
      bottomSheet: Container(
        height: 50.w,
        width: double.infinity,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            backgroundColor: AppColors.blue_button,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(22.r),
            ),
          ),
          onPressed: controller.complete,
          child: Text(
            '确定',
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
